import 'package:flutter/material.dart';

import '../../../common/utils/shortcut.dart';
import '../../resources/resources.dart';
import '../my_div.dart';
import '../my_text.dart';

/// 弹窗主题
class MyDialogTheme {
  static Color backgroundColor = const Color(0xfff0f0f0); //背景色
  static Color? dividerColor; //分割线颜色
  static Color? titleTextColor; //标题文字颜色
  static Color? contentTextColor; //内容文字颜色
  static Color actionTextColor = const Color(0xFF007AFF); //按钮文字颜色
  static Color? barrierColor; //遮罩层颜色

  static double titleFontSize = 17; //标题文字大小
  static double contentFontSize = 13; //内容文字大小
  static double actionFontSize = 17; //按钮文字大小

  static TextAlign contentTextAlign = TextAlign.start; //内容文字对齐方式

  static FontWeight titleFontWeight = FontWeight.bold; //标题文字加粗
  static FontWeight contentFontWeight = FontWeight.normal; //内容文字加粗
}

/// 弹窗
class MyDialog extends StatelessWidget {
  /// 弹窗底部操作按钮列表
  final List<MyDialogAction> actions;

  /// 弹窗标题文本
  final String? title;

  /// 弹窗内容文本
  final String? content;

  /// 弹窗背景颜色
  final Color? backgroundColor;

  /// 分割线颜色
  final Color? dividerColor;

  /// 操作按钮排列方向，水平或垂直
  final Axis actionsDirection;

  /// 是否将弹窗定位在屏幕顶部中央
  final bool positionTopCenter;

  /// 内容文本对齐方式
  final TextAlign? contentTextAlign;

  /// 顶部自定义Widget，显示在标题之上
  final Widget? topWidget;

  /// 标题自定义Widget，替代title文本
  final Widget? titleWidget;

  /// 内容自定义Widget，替代content文本
  final Widget? contentWidget;

  /// 是否启用国际化翻译
  final bool i18n;

  const MyDialog({
    super.key,
    this.title,
    this.content,
    this.backgroundColor,
    this.dividerColor,
    this.contentTextAlign,
    this.topWidget,
    this.titleWidget,
    this.contentWidget,
    required this.actions,
    this.actionsDirection = Axis.horizontal,
    this.positionTopCenter = false,
    this.i18n = true,
  });

  Widget actionsWidget() {
    List<Widget> list = [];

    if (actionsDirection == Axis.horizontal) {
      for (int i = 0; i < actions.length; i++) {
        if (i != 0) {
          list.add(MyDiv.verticalOnePixel(
            color: dividerColor ?? MyDialogTheme.dividerColor,
          ));
        }
        list.add(Expanded(child: _MyDialogActionWidget(action: actions[i])));
      }
      return SizedBox(
        height: 44,
        child: Row(
          children: list,
        ),
      );
    } else {
      for (int i = 0; i < actions.length; i++) {
        if (i != 0) {
          list.add(MyDiv.onePixel(
            color: dividerColor ?? MyDialogTheme.dividerColor,
          ));
        }
        list.add(_MyDialogActionWidget(action: actions[i]));
      }
      return Column(
        mainAxisSize: MainAxisSize.min,
        children: list,
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(left: 40, right: 40, top: 20, bottom: 20),
      child: Column(
        children: [
          const Spacer(),
          ConstrainedBox(
            constraints: const BoxConstraints(
              maxWidth: 270.0, // 设置最大宽度
            ),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(12.0),
              child: Material(
                color: backgroundColor ?? MyDialogTheme.backgroundColor,
                //borderRadius: BorderRadius.circular(12),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Container(
                      padding: const EdgeInsets.only(
                          left: 16, right: 16, top: 16, bottom: 16),
                      //圆角

                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          topWidget ?? const SizedBox.shrink(),
                          //contentWidget,
                          titleWidget ??
                              Container(
                                child: title != null
                                    ? MyText(
                                        title!,
                                        size: MyDialogTheme.titleFontSize,
                                        color: MyDialogTheme.titleTextColor,
                                        textAlign: contentTextAlign ??
                                            TextAlign.center,
                                        weight: MyDialogTheme.titleFontWeight,
                                        i18n: i18n,
                                      )
                                    : Container(),
                              ),
                          gapv(4),
                          contentWidget ??
                              Container(
                                child: content != null
                                    ? MyText(
                                        content!,
                                        size: MyDialogTheme.contentFontSize,
                                        color: MyDialogTheme.contentTextColor,
                                        textAlign: contentTextAlign ??
                                            MyDialogTheme.contentTextAlign,
                                        i18n: i18n,
                                        weight: MyDialogTheme.contentFontWeight,
                                      )
                                    : Container(),
                              ),
                        ],
                      ),
                      // actions: actions, // ?? <Widget>[cancelButton, confirmButton],
                      //buttonPadding: EdgeInsets.zero,
                    ),

                    //const MyDiv(thickness: 0.5,),

                    MyDiv.onePixel(
                      color: dividerColor ?? MyDialogTheme.dividerColor,
                    ),

                    actionsWidget(),
                  ],
                ),
              ),
            ),
          ),
          Spacer(
            flex: positionTopCenter ? 3 : 1,
          ),
        ],
      ),
    );
  }

  Future<T?> show<T>({bool barrierDismissible = true, Color? barrierColor}) {
    return openDialog(this,
        barrierDismissible: barrierDismissible,
        barrierColor: barrierColor ?? MyDialogTheme.barrierColor);
  }
}

class _MyDialogActionWidget extends StatelessWidget {
  final MyDialogAction action;

  const _MyDialogActionWidget({required this.action});

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: action.onTap,
      child: Container(
        //padding: const EdgeInsets.only(top: 2, bottom: 2),

        height: 44,
        width: double.infinity,
        alignment: Alignment.center,
        child: MyText(
          action.text,
          size: action.fontSize ?? MyDialogTheme.actionFontSize,
          color: action.textColor ?? MyDialogTheme.actionTextColor,
          textAlign: TextAlign.center,
          weight: action.bold ? FontWeight.bold : FontWeight.normal,
        ),
      ),
    );
  }
}

class MyDialogAction {
  final String text;
  final Color? textColor;
  final Color? backgroundColor;
  final Color? borderColor;
  final double? fontSize;
  final bool bold;
  final VoidCallback? onTap;

  const MyDialogAction({
    required this.text,
    this.textColor,
    this.backgroundColor,
    this.borderColor,
    this.onTap,
    this.fontSize,
    this.bold = false,
  });

  factory MyDialogAction.cancel({VoidCallback? onTap}) {
    return MyDialogAction(
        text: "取消",
        onTap: onTap ??
            () {
              back();
            },
        bold: true);
  }
  factory MyDialogAction.delete({required VoidCallback onTap}) {
    return MyDialogAction(
      text: "删除",
      textColor: Colors.red,
      onTap: onTap,
    );
  }

  factory MyDialogAction.confirm({required VoidCallback onTap}) {
    return MyDialogAction(
      text: "确定",
      onTap: onTap,
    );
  }
}
